/**
 * 分页对象
 * @Author: linzy
 * @Date: 2022-05-24
 * @function usePagination
 * @param {object} tableForm - 表格监听数据对象
 * @param {object} tableFormSet - 表格监听数据设置方法
 * @param {object} tableData - 表格数据 格式为 {count: xxx,items: []}
 * @returns {object} 分页对象 - antd组件pagination对象
 * @example <caption>用来useTable表格封装中，当然也可以单独使用</caption>
 * const pagination = usePagination({ tableForm, tableData, tableFormSet });
 */
import { useMemo } from "react";
import { setDefaultAttrs } from "@/hooks/tools";
export default (props = {}) => {
  const { tableForm, tableFormSet, tableData, paginationProps = {} } = props;

  const paginationObj = useMemo(() => {
    setDefaultAttrs(paginationProps, {
      current: tableForm.page,
      pageSize: tableForm.limit,
      showSizeChanger: true,
      total: tableData?.count,
      showTotal(total) {
        return `共${total}条数据`;
      },
      onChange(page) {
        tableFormSet((state) => ({ ...state, page }));
      },
      onShowSizeChange(current, size) {
        tableFormSet((state) => ({ ...state, page: 1, limit: size }));
      },
      pageSizeOptions: ["15", "20", "30", "40"],
    });
    return paginationProps;
  }, [tableForm, tableData, tableFormSet]);
  return paginationObj;
};
